﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.IO;
using System.Collections.ObjectModel;

namespace Projet_WPF.ViewModel
{
    class ControlViewModel : BaseViewModel
    {
        #region variables
        private Model.User _userConnected;
        private Model.User _selectedUser = null;
        private Model.Patient _selectedPatient = null;
        private bool _visibleRole;
        private bool _closeSignal;

        private string _lastDateObservationPatient = "";

        private ObservableCollection<Model.User> _listUsers;
        private ObservableCollection<Model.Patient> _listPatients;

        private DataAccess.Patient _dataAccessPatient;
        private DataAccess.User _dataAccessUser;

        private string _loginUser;
        private string _passUser;
        private string _nameUser;
        private string _firstnameUser;
        private string _roleUser;
        private string _pathPictureUser;
        private Byte[] _pictureUser;

        private string _namePatient;
        private string _firstnamePatient;
        private DateTime _birthdayPatient;
        private DateTime _endDatePatient;

        #endregion

        #region commandes
        private ICommand _addUserCommand;
        private ICommand _deleteUserCommand;
        private ICommand _addPatientCommand;
        private ICommand _deletePatientCommand;
        private ICommand _showObservationsCommand;
        private ICommand _searchImageUserCommand;
        private ICommand _deconnexionCommand;
        private ICommand _closeCommand;
        #endregion

        #region getter / setter

        public ICommand CloseCommand
        {
            get { return _closeCommand; }
            set { _closeCommand = value; }
        }

        public ICommand DeconnexionCommand
        {
            get { return _deconnexionCommand; }
            set { _deconnexionCommand = value; }
        }

        public ICommand AddUserCommand
        {
            get { return _addUserCommand; }
            set { _addUserCommand = value; }
        }

        public ICommand DeleteUserCommand
        {
            get { return _deleteUserCommand; }
            set { _deleteUserCommand = value; }
        }

        public ICommand SearchImageUserCommand
        {
            get { return _searchImageUserCommand; }
            set { _searchImageUserCommand = value; }
        }

        public ICommand AddPatientCommand
        {
            get { return _addPatientCommand; }
            set { _addPatientCommand = value; }
        }

        public ICommand DeletePatientCommand
        {
            get { return _deletePatientCommand; }
            set { _deletePatientCommand = value; }
        }

        public ICommand ShowObservationsCommand
        {
            get { return _showObservationsCommand; }
            set { _showObservationsCommand = value; }
        }

        public bool CloseSignal
        {
            get { return _closeSignal; }
            set
            {
                if (_closeSignal != value)
                {
                    _closeSignal = value;
                    OnPropertyChanged("CloseSignal");
                }
            }
        }

        public bool VisibleRole
        {
            get { return _visibleRole; }
            set { _visibleRole = value; }
        }

        public Model.User UserConnected
        {
            get { return _userConnected; }
            set
            {
                if (_userConnected != value)
                {
                    _userConnected = value;
                    OnPropertyChanged("UserConnected");
                }
            }
        }

        public ObservableCollection<Model.User> ListUsers
        {
            get { return _listUsers; }
            set
            {
                if (_listUsers != value)
                {
                    _listUsers = value;
                    OnPropertyChanged("ListUsers");
                }
            }
        }

        public ObservableCollection<Model.Patient> ListPatients
        {
            get { return _listPatients; }
            set
            {
                if (_listPatients != value)
                {
                    _listPatients = value;
                    OnPropertyChanged("ListPatients");
                }
            }
        }

        public Model.User SelectedUser
        {
            get { return _selectedUser; }
            set
            {
                if (_selectedUser != value)
                {
                    _selectedUser = value;
                    OnPropertyChanged("SelectedUser");
                }
            }
        }

        public Model.Patient SelectedPatient
        {
            get { return _selectedPatient; }
            set
            {
                if (_selectedPatient != value)
                {
                    _selectedPatient = value;

                    if (_selectedPatient != null)
                    {
                        _selectedPatient.Observations = _dataAccessPatient.GetPatient(_selectedPatient.Id).Observations;

                        if (_selectedPatient.Observations.Count != 0)
                        {
                            DateTime dateTmp = _selectedPatient.Observations.First().Date;

                            foreach (var observation in _selectedPatient.Observations)
                            {
                                if (DateTime.Compare(dateTmp, observation.Date) <= 0)
                                    dateTmp = observation.Date;
                            }

                            _lastDateObservationPatient = dateTmp.ToString();
                        }
                        else
                            _lastDateObservationPatient = "Aucune observation pour l'instant";
                    }
                    else
                    {
                        if (_selectedPatient == null)
                            _lastDateObservationPatient = "";
                        else
                            _lastDateObservationPatient = "Aucune observation pour l'instant";
                    }

                    OnPropertyChanged("LastDateObservationPatient");
                    OnPropertyChanged("SelectedPatient");
                }
            }
        }

        public string LastDateObservationPatient
        { 
            get { return _lastDateObservationPatient;   }
            set { _lastDateObservationPatient = value;  }
        }

        public string LoginUser
        {
            get { return _loginUser; }
            set
            {
                if (_loginUser != value)
                {
                    _loginUser = value;
                    OnPropertyChanged("LoginUser");
                }
            }
        }

        public string PassUser
        {
            get { return _passUser; }
            set
            {
                if (_passUser != value)
                {
                    _passUser = value;
                    OnPropertyChanged("PassUser");
                }
            }
        }

        public string NameUser
        {
            get { return _nameUser; }
            set
            {
                if (_nameUser != value)
                {
                    _nameUser = value;
                    OnPropertyChanged("NameUser");
                }
            }
        }

        public string FirstnameUser
        {
            get { return _firstnameUser; }
            set
            {
                if (_firstnameUser != value)
                {
                    _firstnameUser = value;
                    OnPropertyChanged("FirstnameUser");
                }
            }
        }

        public string RoleUser
        {
            get { return _roleUser; }
            set
            {
                if (_roleUser != value)
                {
                    _roleUser = value;
                    OnPropertyChanged("RoleUser");
                }
            }
        }

        public string PathPictureUser
        {
            get { return _pathPictureUser; }
            set
            {
                if (_pathPictureUser != value)
                {
                    _pathPictureUser = value;
                    OnPropertyChanged("PathPictureUser");
                }
            }
        }

        public Byte[] PictureUser
        {
            get { return _pictureUser; }
            set
            {
                if (_pictureUser != value)
                {
                    _pictureUser = value;
                    OnPropertyChanged("PictureUser");
                }
            }
        }

        public string NamePatient
        {
            get { return _namePatient; }
            set
            {
                if (_namePatient != value)
                {
                    _namePatient = value;
                    OnPropertyChanged("NamePatient");
                }
            }
        }

        public string FirstnamePatient
        {
            get { return _firstnamePatient; }
            set
            {
                if (_firstnamePatient != value)
                {
                    _firstnamePatient = value;
                    OnPropertyChanged("FirstnamePatient");
                }
            }
        }

        public DateTime BirthdayPatient
        {
            get { return _birthdayPatient; }
            set
            {
                if (_birthdayPatient != value)
                {
                    _birthdayPatient = value;
                    OnPropertyChanged("BirthdayPatient");
                }
            }
        }

        public DateTime EndDatePatient
        {
            get { return _endDatePatient; }
            set
            {
                if (_endDatePatient != value)
                {
                    _endDatePatient = value;
                    OnPropertyChanged("EndDatePatient");
                }
            }
        }

        #endregion

        public ControlViewModel(Model.User user, bool visible)
        {
            DisplayName = "Fenêtre de contrôle";
            LoginUser = "";
            PassUser = "";
            NameUser = "";
            FirstnameUser = "";
            PathPictureUser = "";
            RoleUser = "";
            PictureUser = null;
            NamePatient = "";
            FirstnamePatient = "";
            BirthdayPatient = DateTime.Today;
            EndDatePatient = DateTime.Today;

            VisibleRole = visible;

            _dataAccessUser = new DataAccess.User();
            _dataAccessPatient = new DataAccess.Patient();
            UserConnected = user;
            ListUsers = new ObservableCollection<Model.User>();

            foreach (var item in _dataAccessUser.GetListUser())
            {
                if (item.Login != UserConnected.Login)
                    ListUsers.Add(item);
            }

            ListPatients = new ObservableCollection<Model.Patient>(_dataAccessPatient.GetListPatient());

            AddUserCommand = new RelayCommand(param => AddUser());
            DeleteUserCommand = new RelayCommand(param => DeleteUser());
            AddPatientCommand = new RelayCommand(param => AddPatient());
            DeletePatientCommand = new RelayCommand(param => DeletePatient());
            ShowObservationsCommand = new RelayCommand(param => ShowObservations());
            SearchImageUserCommand = new RelayCommand(param => SearchImage());
            DeconnexionCommand = new RelayCommand(param => Deconnexion(), param => true);
            CloseCommand = new RelayCommand(param => Close(), param => true);
        }

        private void Close()
        {
            CloseSignal = true;
        }

        private void AddUser()
        {
            if (LoginUser != "" && PassUser != "" && NameUser != "" && FirstnameUser != "" && RoleUser != "" && PathPictureUser != "" && PictureUser != null)
            {
                Model.User user = Model.User.CreateUser(LoginUser, PassUser, NameUser, FirstnameUser, PictureUser, RoleUser, false);
                if (_dataAccessUser.AddUser(user))
                {
                    ListUsers.Add(user);
                    OnPropertyChanged("ListUsers");

                    LoginUser = "";
                    PassUser = "";
                    NameUser = "";
                    FirstnameUser = "";
                    RoleUser = "";
                    PictureUser = null;
                    PathPictureUser = "";
                }
            }
        }

        private void SearchImage()
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                StreamReader sr = new StreamReader(dlg.FileName);
                BinaryReader read = new BinaryReader(sr.BaseStream);
                PictureUser = read.ReadBytes((int)sr.BaseStream.Length);
                PathPictureUser = dlg.FileName;
            }
        }

        private void DeleteUser()
        {
            if (SelectedUser != null)
            {
                if (_dataAccessUser.DeleteUser(SelectedUser.Login))
                {
                    ListUsers.Remove(SelectedUser);
                    OnPropertyChanged("ListUsers");
                }
            }
        }

        private void AddPatient()
        {
            if (NamePatient != "" && FirstnamePatient != "")
            {
                Model.Patient patient = Model.Patient.CreatePatient(NamePatient, FirstnamePatient, BirthdayPatient);

                if (_dataAccessPatient.AddPatient(patient))
                {
                    ListPatients.Add(patient);
                    OnPropertyChanged("ListPatients");

                    NamePatient = "";
                    FirstnamePatient = "";
                    BirthdayPatient = DateTime.Today;
                }
            }
        }

        private void DeletePatient()
        {
            if (SelectedPatient != null)
            {
                if (_dataAccessPatient.DeletePatient(SelectedPatient.Id))
                {
                    ListPatients.Remove(SelectedPatient);
                    OnPropertyChanged("ListPatients");
                }
            }
        }

        private void ShowObservations()
        {
            if (SelectedPatient != null)
            {
                View.ObservationsView window = new View.ObservationsView();
                ViewModel.ObservationsViewModel vm = new ObservationsViewModel(SelectedPatient, VisibleRole);

                window.DataContext = vm;
                window.Show();
            }
        }

        private void Deconnexion()
        {
            _dataAccessUser.Disconnect(UserConnected.Login);

            View.FirstView window = new View.FirstView();
            ViewModel.FirstViewModel vm = new FirstViewModel();

            window.DataContext = vm;
            window.Show();

            CloseSignal = true;
        }
    }
}
